package cn.com.connected.base.params.page;

import com.github.pagehelper.Page;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @param <T>
 * @author vitem
 */
@Data
public class PageResult<T> implements Serializable {

    /**
     * 当前页
     */
    private int pageIndex;

    /**
     * 每页的数量
     */
    private int pageSize;

    /**
     * 当前页的数量
     */
    private int size;

    /**
     * 总记录数
     */
    private long totalCount;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 最大限制数量
     */
    private transient long totalLimitCount;

    /**
     * 最大限制页
     */
    private transient int totalLimitPage;

    private boolean noMore = false;

    /**
     * 结果集
     */
    private List<T> resultData;

    //    /**
//     * 扩展属性
//     */
    private Object extMeta;

    public PageResult() {

    }

    /**
     * 包装Page对象
     *
     * @param list
     */
    public PageResult(List<T> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        if (list instanceof Page){
            buildData((Page) list);
        }else{
            this.pageIndex = 1;
            this.pageSize = list.size();
            this.totalPage = this.pageSize > 0 ? 1 : 0;
            this.resultData = list;
            this.size = list.size();
            this.totalCount = list.size();
        }
    }


    public PageResult limitCount() {
        this.totalPage = totalLimitPage;
        this.totalCount = totalLimitCount;
        return this;
    }

    /**
     * @Description 追加分页，针对对大数据量分页，对count本身不关心的，瀑布流翻页做的
     * @author 邵明生
     * @date 2020/9/28 18:17
     **/
    public PageResult<T> appendPage(List<T> datas,PageParam pageParam){
        this.noMore = CollectionUtils.isEmpty(datas);
        this.resultData = datas;
        this.pageIndex = pageParam.getPageIndex() + (CollectionUtils.isEmpty(datas)?1:0);
        this.pageSize = pageParam.getPageSize()>25?PageParam.DEFAULT_PAGE_SIZE:pageParam.getPageSize();
        this.size = datas.size();
        return this;
    }

    public PageResult<T> appendPage(List<T> datas,PageParam pageParam,boolean noMore){
        this.noMore = noMore;
        this.resultData = datas;
        this.pageIndex = pageParam.getPageIndex() + (CollectionUtils.isEmpty(datas)?1:0);
        this.pageSize = pageParam.getPageSize()>25?PageParam.DEFAULT_PAGE_SIZE:pageParam.getPageSize();
        this.size = datas.size();
        return this;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("PageInfo{");
        sb.append("pageIndex=").append(pageIndex);
        sb.append(", pageSize=").append(pageSize);
        sb.append(", size=").append(size);
        sb.append(", totalCount=").append(totalCount);
        sb.append(", totalPage=").append(totalPage);
        sb.append('}');
        return sb.toString();
    }
    public PageResult(Page page) {
        buildData(page);
    }
    private void buildData(Page page){
        this.pageIndex = page.getPageNum();
        this.pageSize = page.getPageSize();
        this.totalPage = page.getPages();
        this.resultData = page;
        this.size = page.size();
        this.totalCount = page.getTotal();

        this.totalLimitPage = page.getPages()>25?25:page.getPages();
        this.totalLimitCount = page.getTotal()>totalLimitPage*pageSize?totalLimitPage*pageSize:page.getTotal();
    }
    public void setResultData(List<T> resultData) {
        this.resultData = resultData;
    }
}
